package com.stone.sys.service;

/**
 * <pre>
 * 用户信息操作Action
 * </pre>
 */

import java.util.HashMap;
import java.util.List;

import com.stone.para.IConstant;
import com.stone.sys.pojo.TUser;
import com.stone.sys.pojo.TUserRole;
import com.stone.utils.seq.SeqUtil;

import frm.ssh.exception.AppException;
import frm.ssh.service.BaseService;

public class UserService extends BaseService {
	
	/**
	 * 修改密码
	 * @param password
	 * @param id
	 */
	public void updatePassword(String password, Long id) {
		String hql = "update TUser set password = '" + password +"' where id = '" + id + "'";
		super.updateByHQL(hql, null);
	}

	/**
	 * 删除
	 * 
	 * @param ids
	 * @throws AppException
	 */
	public void doDelAll(String ids) {
		// 删除用户
		super.deleteAll(ids, "TUser");
		// 删除用户角色
		super.deleteByDHql(" userId in (" + ids + ")", null, "TUserRole");

	}
	/**
	 * 用户授权及取消
	 */
	@SuppressWarnings("unchecked")
	public void saveUserRole(String userId,String roleId, String flag) throws AppException {
		
		// 要操作的角色ID数组
		String[] sid = roleId.split(",");
		
		// 授权时
		if ("1".equals(flag)) {
			
			// 人员已有的角色
			List<HashMap<String,Object>> list = this.getListByDHql("ur.id as id,ur.roleId as roleId", 
											" ur.userId = ?",// + and rm.roleId = '" + id + "'",
											new Object[]{Long.parseLong(userId)},
											"",
											"TUserRole ur ");
		
			boolean f = false;
			
			// 需要授权的角色ID数组
			for (String s : sid) {
				
				f = false;
				
				// 人员如果已有该角色不再授权
				for (HashMap<String,Object> m : list) {
					if (s.trim().equals(String.valueOf(m.get("roleId")))) {
						f = true;	
						break;
					}
				}
				// 没有的授权
				if (!f) {
					TUserRole rm = new TUserRole();
					rm.setRoleId(Long.parseLong(s.trim()));
					rm.setUserId(Long.parseLong(userId));
					super.save(rm);
				}
			}
			
			// 取消授权
		} else {
			
			if(roleId.endsWith(",")) {
				roleId = roleId.substring(0,roleId.length()-1);
			}
			super.deleteByDHql(" roleId in (" + roleId + ") and userId = '" + userId + "'" , null, "TUserRole");
		}
	}
	
	/**
	 * 通过用户权限取得按钮
	 * @return
	 */
	public List<HashMap<String,Object>> getButtonByUserRole(TUser user, String qxbtn) {
		
		List<HashMap<String,Object>> roleBtn = super.getListByDHql(
				"m.iconCls as iconCls,m.url as url", 
				"m.id = rm.menuId and rm.roleId = ur.roleId and ur.userId = ? and m.parentID = ? group by m.iconCls,m.url", 
				new Object[]{user.getId(),Long.parseLong(qxbtn)}, 
				"",
				"TMenu m,TRoleMenu rm,TUserRole ur ");
		
		return roleBtn;
	}

	/**
	 * 执行Hql查询查询
	 * 
	 * @param hql  查询语句
	 * @return List 结果集
	 */
	public List findListByHql(String hql)  {
			return super.findListByHql(hql);
	}
	
	
	/**
	 * 根据ID查询记录
	 * 
	 * @param id ID
	 * @param objectFullName POJO名 包括包路径
	 * @return Object
	 */
	public Object findById(Long id, String objectFullName)  {
		return super.findById(id, objectFullName);
	}
	
	
	/**
	 * 保存pojo对象
	 * 
	 * @param transientInstance pojo对象
	 */
	public void save(Object transientInstance)  {
			super.save(transientInstance);
	}
	/**
	 * 修改记录
	 * 
	 * @param transientInstance pojo对象
	 */
	public void update(Object transientInstance)  {
			super.update(transientInstance);
	}
	
	/**
	 * 修改记录
	 * 
	 * @param transientInstance pojo对象
	 */
	public void doDelAll(final String ids,final String objectSimpleName)  {
			super.doDelAll(ids,objectSimpleName);
	}
	
	/**
	 * 通过SQL查询列表，返回每条是一个HashMap<String,Object>
	 * @param sql 查询SQL语句
	 * @return list 返回列表
	 */
	public List<HashMap<String,Object>> findListBySql(String sql){
	
		List<HashMap<String,Object>> list =  (List<HashMap<String,Object>>) super.findListBySql(sql);
		return list;
	}

	
	/**
	 * 此方法是用来动态获取列表，可以多表关联
	 * 
	 * @param cols 查询列名
	 * @param conditions 查询条件
	 * @param vals 查询条件对应值
	 * @return clause order by, group by,having等子句
	 * @param objectSimpleName 
	 */
	public List getListByDHql(final String cols, final String conditions,
			final Object[] vals, final String clause,
			final String objectSimpleName)  {
		
		return super.getListByDHql(cols, conditions, vals, clause, objectSimpleName);
	}
	
	/**
	 * 根据ID删除记录
	 * 
	 * @param id 删除记录的ID
	 * @param objectSimpleName
	 */
	public void delete(Long id, String objectSimpleName){
			super.delete(id, objectSimpleName);
	}
	/**
	 * 通过HQL修改记录
	 * 
	 * @param hql 修改的HQL语句
	 * @param values HQL语句对应值
	 */
	public void updateByHQL(String hql, Object[] values){
		super.updateByHQL(hql,values);
	}
}
